package com.yiwei.hdb.activity;

import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yiwei.hdb.R;
import com.yiwei.hdb.api.callback.HelpDetailContract;
import com.yiwei.hdb.api.requestInfo.HelpDetailRequest;
import com.yiwei.hdb.constant.Const;
import com.yiwei.hdb.model.ContentDetailModel;
import com.yiwei.hdb.model.MessageDetailModel;
import com.yiwei.hdb.util.ToastUtil;
import com.yiwei.hdb.view.CircularProgress;

/**
 * 帮助详情界面
 * Created by Master.Dong on 16/4/10.
 */
public class HelpDetailActivity extends BaseActivity implements HelpDetailContract.View {

    private TextView title, content;
    private LinearLayout contentView;
    private String id;
    private int type;
    private CircularProgress loading;
    private HelpDetailRequest api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.help_detail_activity);
        baseInitView(this, "HelpDetailActivity", Const.ViewType.ACTIVITY);
        initView();

    }

    private void initView() {
        api = HelpDetailRequest.create();
        api.attachView(this);
        setTitleText(getResources().getString(R.string.help_detail));
        setBackIntent();

        id = getIntent().getStringExtra("id");
        type = getIntent().getIntExtra("type", Const.FragmentType.HELP);

        loading = (CircularProgress) findViewById(R.id.progressBar);
        title = (TextView) findViewById(R.id.title);
        content = (TextView) findViewById(R.id.help_content);
        contentView = (LinearLayout) findViewById(R.id.content_ll);
        contentView.setVisibility(View.INVISIBLE);

        if (type == Const.FragmentType.MESSAGE) getMessageInfo();
        else getHelpInfo();
    }

    /**
     * 根据id获取帮助信息
     */
    private void getHelpInfo() {
        api.getHelpDetailInfo(id);
    }

    /**
     * 根据id获取消息中心信息
     */
    private void getMessageInfo() {
        api.getMessageDetailInfo(id);
    }


    /**
     * 是否显示加载框
     *
     * @param b
     */
    private void showLoading(boolean b) {
        if (b) {
            if (loading != null) loading.setVisibility(View.VISIBLE);
        } else {
            if (loading != null) loading.setVisibility(View.GONE);
        }
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        showLoading(false);
    }

    @Override
    public void onRequestStart() {
        showLoading(true);
    }


    @Override
    public void getHelpDetailInfoSuccess(ContentDetailModel model) {
        title.setText(model.getTitle());
        Spanned spanned = Html.fromHtml(model.getContent().toString());
        content.setText(spanned);
        contentView.setVisibility(View.VISIBLE);
    }

    @Override
    public void getHelpDetailInfoFail(String msg) {
        contentView.setVisibility(View.GONE);
    }

    @Override
    public void getMessageDetailInfoSuccess(MessageDetailModel model) {
        title.setText(model.getData().getTitle());
        Spanned spanned = Html.fromHtml(model.getData().getContent().toString());
        content.setText(spanned);
        contentView.setVisibility(View.VISIBLE);
    }

    @Override
    public void getMessageDetailInfoFail(String msg) {
        contentView.setVisibility(View.GONE);
        ToastUtil.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        api.detachView();
        api = null;
    }
}
